﻿/*
 * Created by: Laurent Kempé - http://weblogs.asp.net/lkempe/
 * Created: Wednesday, August 16, 2006
 */

using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using WindowsLive.Writer.Api;

namespace TechHeadBrothers.Rss
{
    [WriterPlugin
        ("030D77D8-3143-41dd-9147-D524DBDD601F",
        "Windows Media Player Currently Playing",
        ImagePath = "Images.WMPCurrentlyPlayingPlugin.png",
        PublisherUrl = "http://weblogs.asp.net/lkempe/",
        Description = "Embed Windows Media Player Currently Playing Title in your blog posts")]
    
    [InsertableContentSourceAttribute("WMP Currently Playing")]
    public class WMPCurrentlyPlayingPlugin : ContentSource
    {
        /// <summary>
        /// Creates the content.
        /// </summary>
        /// <param name="dialogOwner">The dialog owner.</param>
        /// <param name="newContent">The new content.</param>
        /// <returns></returns>
        public override DialogResult CreateContent(IWin32Window dialogOwner,
                                                   ref string newContent)
        {
            newContent = GetContent();

            return DialogResult.OK;
        }

        /// <summary>
        /// Gets the content out of the registry (WMP Blogging Plugin).
        /// </summary>
        /// <returns></returns>
        private string GetContent()
        {
            RegistryKey currentMetadata =
                Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\MediaPlayer\CurrentMetadata", false);

            if (currentMetadata == null)
                return "";

            string title = currentMetadata.GetValue("Title", "No Title") as string;
            string album = currentMetadata.GetValue("Album", "No Album") as string;
            string author = currentMetadata.GetValue("Author", "No Author") as string;
            string duration = currentMetadata.GetValue("durationString", "No Duration") as string;

            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"<p>&nbsp;</p><p class='media'>[ Currently Playing : {0} - {1} - {2} ({3}) ]</p>",
                                 title,
                                 author,
                                 album,
                                 duration);

            return builder.ToString();
        }
    }
}